package org.dekuan.ChinaRegionJava.controllers.v1;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.dekuan.ChinaRegionJava.Entity.City;
import org.dekuan.ChinaRegionJava.mapper.CityMapper;
import org.dekuan.ChinaRegionJava.models.RestChinaRegionCityListResponse;
import org.dekuan.ChinaRegionJava.models.RestChinaRegionDistrictListResponse;
import org.dekuan.ChinaRegionJava.models.RestChinaRegionProvinceListResponse;
import org.dekuan.ChinaRegionJava.services.ChinaRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Api( tags = "中国行政区域" )
@RequestMapping( "/api/v1/chinaRegion/" )
@Slf4j
public class ChinaRegionControllerV1
{
	@Autowired
	private ChinaRegionService chinaRegionService;


	@ApiOperation( value = "获取省份列表", notes = "-" )
	@RequestMapping( value = "/province/list", method = { RequestMethod.GET } )
	@ResponseBody
	public RestChinaRegionProvinceListResponse getProvinceList
		(
			HttpServletRequest request,
			HttpServletResponse response
		)
	{
		return chinaRegionService.getRestProvinceList();

	}
	@Autowired
	CityMapper cityMapper;

	@ApiOperation( value = "获取城市列表", notes = "-" )
	@RequestMapping( value = "/city/list/{provinceId}", method = { RequestMethod.GET } )
	@ResponseBody
	public 	Map<String,Object> getCityList
		(
			HttpServletRequest request,
			HttpServletResponse response,
			@PathVariable String provinceId
		)
	{
		// 把查到的插入到citys表中
		List<String> specCity = new ArrayList<>();
		specCity.add("500100000000");
		specCity.add("110100000000");
		specCity.add("120100000000");
		specCity.add("310100000000");


		List<String> pId = chinaRegionService.getRestProvinceList().getBody().getProvinceList()
				.stream()
				.map(region -> region.getId().toString()) // 获取id字段并转为字符串
				.collect(Collectors.toList());
		pId.removeAll(specCity);
		pId.remove("110100000000");
		pId.remove("500100000000");
		pId.remove("310100000000");
		pId.remove("120100000000");
		Map ciMap = new LinkedHashMap();
		System.out.println("处理后"+pId);
		for(String id:pId){

			List<ChinaRegionService.RegionNode> cs = chinaRegionService.getRestCityList(id).getBody().getCityList();
			List<City> cityList = new ArrayList<>();

			for (ChinaRegionService.RegionNode regionNode : cs) {
				if (regionNode.getName().equals("辖区")) {
					continue; // 跳过当前迭代，继续下一次迭代
				}
				City city = new City();
				city.setId(regionNode.getId());

				String name = regionNode.getName();
				name = name.replace("省", "")
						.replace("市", "")
						.replace("地区", "")
						.replace("自治区", "");
				city.setName(name);

				String name1 = regionNode.getProvince();
				name1 = name1.replace("省", "")
						.replace("市", "")
						.replace("自治区", "");
				city.setProvince(name1);

//			city.setProvince(regionNode.getProvince());
//			city.setName(regionNode.getName());
				cityList.add(city);
				ciMap.put(city.getName(),city);
				System.out.println("city.getName()"+ciMap);
				if (!name.equals("辖区")) { // 仅当不是"辖区"时才添加到ciMap中
					ciMap.put(name, city);
				}
			}

			// 使用MyBatis Plus的insertBatch方法批量插入数据
			cityMapper.insertBatch(cityList);

			System.out.println(ciMap);



		}

		for(String cityId:specCity){
			//		specCity.add("500100000000");
			//		specCity.add("110100000000");
			//		specCity.add("120100000000");
			//		specCity.add("310100000000");
			String pro = null;
			if(cityId=="500100000000"){
				 pro ="重庆";
			}
			if(cityId=="110100000000"){
				 pro ="北京";
			}
			if(cityId=="120100000000"){
				 pro ="天津";
			}
			if(cityId=="310100000000"){
				 pro ="上海";
			}


			List<ChinaRegionService.RegionNode> districtList = chinaRegionService.getRestDistrictList(cityId).getBody().getDistrictList();
			List<City> cityList = new ArrayList<>();

			for (ChinaRegionService.RegionNode regionNode : districtList) {
				City city = new City();
				city.setId(regionNode.getId());

				String name = regionNode.getName();
				name = name.replace("省", "")
						.replace("市", "");
//						.replace("区", "")
//						.replace("自治区", "");
				city.setName(name);

				String name1 = regionNode.getProvince();

				name1 = name1.replace("省", "")
						.replace("市", "")
						.replace("自治区", "");
				city.setProvince(pro);

				cityList.add(city);
				ciMap.put(city.getName(),city);
			}

			// 使用MyBatis Plus的insertBatch方法批量插入数据
			cityMapper.insertBatch(cityList);

			System.out.println("插入city直辖市数据成功");


		}
		cityMapper.delete(new QueryWrapper<City>().eq("name", "辖区"));
		return ciMap;

	}


	@ApiOperation( value = "获取区域列表", notes = "-" )
	@RequestMapping( value = "/district/list/{cityId}", method = { RequestMethod.GET } )
	@ResponseBody
	public RestChinaRegionDistrictListResponse getDistrictList
			(
					HttpServletRequest request,
					HttpServletResponse response,
					@PathVariable String cityId
			)
	{
		return chinaRegionService.getRestDistrictList( cityId );
	}
}